import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard'),
        meta: {title: '首页',icon: 'dashboard'},
      },
    ]
  },
  {
    path: '/data',
    component: Layout,
    children: [
      {
        path: 'echarts',
        name: 'Echarts',
        component: () => import('@/views/dataset'),
        meta: {title: '数据展示',icon: 'example'},
      },
    ]
  },
  {
    path: '/product',
    name: 'Product',
    component: Layout,
    meta: {title: '商品管理',icon: 'el-icon-goods'},
    children: [
      {
        path: 'tradmark',
        name: 'Tradmark',
        component: () => import('@/views/product/trademark'),
        meta: {title: '品牌管理'},
      },
      {
        path: 'attribute',
        name: 'Attribute',
        component: () => import('@/views/product/attribute'),
        meta: {title: '属性管理'},
      },
      {
        path: 'sku',
        name: 'Sku',
        component: () => import('@/views/product/sku'),
        meta: {title: 'sku管理'},
      },
      {
        path: 'spu',
        name: 'Spu',
        component: () => import('@/views/product/spu'),
        meta: {title: 'spu管理'},
      },
    ]
  },
  {
    name: 'Acl',
    path: '/acl',
    component: Layout,
    redirect: '/acl/user/list',
    meta: {
      title: '权限管理',
      icon: 'el-icon-lock'
    },
    children: [
      {
        name: 'User',
        path: 'user/list',
        component: () => import('@/views/acl/user/list'),
        meta: {
          title: '用户管理',
        },
      },
      {
        name: 'Role',
        path: 'role/list',
        component: () => import('@/views/acl/role/list'),
        meta: {
          title: '角色管理',
        },
      },
      {
        name: 'RoleAuth',
        path: 'role/auth/:id',
        component: () => import('@/views/acl/role/roleAuth'),
        meta: {
          activeMenu: '/acl/role/list',
          title: '角色授权',
        },
        hidden: true,
      },
      {
        name: 'Permission',
        path: 'permission/list',
        component: () => import('@/views/acl/permission/list'),
        meta: {
          title: '菜单管理',
        },
      },
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
